package cn.com.startai.smartadh5.db.db.manager;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import cn.com.startai.smartadh5.db.db.entity.PlayInfo;
import cn.com.startai.smartadh5.db.db.gen.DaoSession;
import cn.com.startai.smartadh5.db.db.gen.PlayInfoDao;
import cn.com.startai.smartadh5.processlogic.entity.bean.player.RemovePlayListBean;

import static android.R.attr.type;

/**
 * @author 尹晓童
 *         邮箱：yinxtno1@yeah.net
 *         时间：2018/1/23
 *         作用：播放信息帮助类（音箱 用）
 */

public class PlayInfoHelper {

    private PlayInfoDao mPlayInfoDao;

    private static class SingleInstance {
        private static final PlayInfoHelper INSTANCE = new PlayInfoHelper();
    }

    public static PlayInfoHelper getInstance() {
        return SingleInstance.INSTANCE;
    }

    private PlayInfoHelper() {
        DaoSession daoSession = DBManager.getDaoSession();
        mPlayInfoDao = daoSession.getPlayInfoDao();
    }

    /**
     * 添加列表
     * @param list 需要添加的数据
     * @param type 类型
     * @param needReplace 是否需要替换
     */
    public void insertList(List<PlayInfo> list, int type, boolean needReplace) {
        if(needReplace) {
            deleteByType(type);
        }
        for (PlayInfo playInfo : list) {
            playInfo.setType(type);
            playInfo.setSourceType(PlayInfo.SOURCE_TYPE_LOCAL);
            long id = mPlayInfoDao.insert(playInfo);
            playInfo.setId(id);
        }
    }


    public void deleteByType(int type){
        mPlayInfoDao.queryBuilder()
                .where(PlayInfoDao.Properties.Type.eq(type))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
    }

    /**
     * 通过 fileId 删除
     * @param fileIds
     */
    public void deleteByFileIds(List<Integer> fileIds) {
        mPlayInfoDao.queryBuilder().where(PlayInfoDao.Properties.FileId.in(fileIds))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
    }

    public void deleteByFileId(int fileId){
        mPlayInfoDao.queryBuilder().where(PlayInfoDao.Properties.FileId.eq(fileId))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
    }

    public List<PlayInfo> queryListByType(int type) {
        return mPlayInfoDao.queryBuilder()
                .where(PlayInfoDao.Properties.Type.eq(type))
                .build()
                .list();
    }
}
